<template>
  <van-row>
    <van-nav-bar
      title="收款管理"
      left-text="返回"
      left-arrow
      @click-left="$router.push('/')"
      @click-right="handleAction"
    >
      <template #right>
        <van-icon name="plus" size="20" />
      </template>
    </van-nav-bar>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-search v-model="page.query.keyword" show-action placeholder="请输入关键字" @search="handleSearch">
        <template #action>
          <div @click="handleSearch">搜索</div>
        </template>
      </van-search>
      <van-dropdown-menu :close-on-click-outside="false">
        <van-dropdown-item v-model="page.query.receiptMethod" :options="receiptMethodOptions" @change="handleSearch" />
        <van-dropdown-item ref="item" title="筛选">
          <van-cell>
            <template #default>
              <van-field
                readonly
                clickable
                :value="receiptDateRange"
                placeholder="选择日期"
                label="收款日期"
                input-align="left"
                @click="showDateRangePicker = true"
              />
            </template>
          </van-cell>
          <div style="padding: 20px 20px;display: flex">
            <van-button style="flex: 1" round @click="handleReset">
              重置
            </van-button>
            <van-button style="flex: 1" type="danger" round @click="handleSearch('0')">
              确定
            </van-button>
          </div>
        </van-dropdown-item>
      </van-dropdown-menu>
<!--      <van-tabs v-model="activeTab" :swipe-threshold="3" @click="handleTabChange">
        <van-tab
          v-for="(item,index) in receiptMethodList"
          :key="index"
          :title="item.stateName+'('+item.count+')'"
        />
      </van-tabs>-->
      <van-list
        v-model="tableLoading"
        :finished="finished"
        finished-text="没有更多了"
        @load="doPage"
      >

        <van-cell v-for="(item,index) in tableData" :key="index" is-link @click="toDetail(item)">
          <template #default>
            <div class="title">
              <div v-html="highlightText(item.customerName + ' ' + item.receiptDate, page.query.keyword)" />
            </div>
            <div class="title right primary">
              <div v-html="highlightText(item.amount, page.query.keyword)" />
            </div>
            <div class="desc">
              <dict-value v-model="item.receiptMethod" dict-key="payment_method" />
            </div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <van-popup v-model="showBatchSubmit" position="right" :style="{ width: '100%', height: '100%' }">
      <receipt-batch-submit-module ref="batchSubmitModule" @close="handleClose" />
    </van-popup>
    <van-popup v-model="showDetail" position="right" :style="{ width: '100%', height: '100%' }">
      <receipt-detail-module ref="detailModule" @close="handleClose" />
    </van-popup>
    <van-calendar v-model="showDateRangePicker" type="range" :min-date="minDate" :show-confirm="false" @confirm="onConfirmDateRange" />
  </van-row>
</template>
<script>
import { selectReceiptPage } from '@/api/receipt'
import { highlightText } from '@/utils/util'
import ReceiptDetailModule from '@/views/receipt/receiptDetail.vue'
import moment from 'moment'
import Moment from 'moment/moment'
import DictValue from '@/components/dictValue.vue'
import ReceiptBatchSubmitModule from '@/views/receipt/receiptBatchSubmit.vue'

export default {
  name: 'Receipt',
  components: { ReceiptBatchSubmitModule, DictValue, ReceiptDetailModule },
  data() {
    return {
      showDetail: false,
      showBatchSubmit: false,
      activeTab: 0,
      tableLoading: false,
      finished: false,
      refreshing: false,
      tableData: [],
      receiptMethodList: [],
      receiptMethodOptions: [
        { text: '全部', value: '-1' },
        { text: '现金收款', value: '0' },
        { text: '微信收款', value: '1' },
        { text: '支付宝收款', value: '2' },
        { text: '银行卡收款', value: '3' }
      ],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          keyword: '',
          receiptMethod: '-1'
        }
      },
      showDateRangePicker: false,
      receiptDateRange: '',
      minDate: moment(new Date()).subtract(30, 'years').toDate()
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    highlightText,
    doPage() {
      this.tableLoading = true
      selectReceiptPage(this.page).then(resp => {
        if (resp.success) {
          if (this.refreshing) {
            this.tableData = []
            this.refreshing = false
          }
          this.tableData = [...this.tableData, ...resp.data.records]
          this.page.total = resp.data.total
          this.finished = this.tableData.length >= resp.data.total
        }
      }).catch(() => {
        this.finished = true
      }).finally(() => {
        this.tableLoading = false
        this.page.page++
      })
      // this.selectReceiptMethodCount()
    },
    handleSearch(type) {
      this.tableData = []
      this.page.page = 1
      this.finished = false
      this.doPage()
      if (type === '0') {
        this.$refs.item.toggle()
      }
    },
    onRefresh() {
      this.page.page = 1
      this.doPage()
    },
    toDetail(row) {
      this.showDetail = true
      this.$nextTick(() => {
        this.$refs.detailModule.init(row.id)
        this.$refs.detailModule.initOrder(row.orderId)
      })
    },
    handleAction() {
      this.showBatchSubmit = true
      this.$nextTick(() => {
        this.$refs.batchSubmitModule.init()
      })
    },
    handleTabChange(row) {
      this.page.query.receiptMethod = (row - 1) + ''
      this.page.page = 1
      this.refreshing = true
      this.doPage()
    },
    /* selectReceiptMethodCount() {
      selectReceiptMethodCount(this.page.query).then(resp => {
        if (resp.success) {
          this.receiptMethodList = resp.data
        }
      })
    }, */
    handleClose() {
      this.showBatchSubmit = false
      this.showDetail = false
      this.handleSearch()
    },
    onConfirmDateRange(date) {
      const [start, end] = date
      this.page.query.receiptDateRange = [Moment(start).format('YYYY-MM-DD'), Moment(end).format('YYYY-MM-DD')]
      this.receiptDateRange = Moment(start).format('YYYY-MM-DD') + '至' + Moment(end).format('YYYY-MM-DD')
      this.showDateRangePicker = false
    },
    handleReset() {
      this.receiptDateRange = ''
      this.page.query.receiptDateRange = []
    }
  }
}
</script>

<style scoped>
.output_quantity {
  display: inline;
  font-size: 20px;
  color: #ff4949;
}

.output_title {
  color: #515a6e;
  display: inline;
  font-size: 16px
}
</style>
